<?php

class Back_RolesController extends Osml_Controller_Admin_Action
{

    public function indexAction ()
    {
        $model = new Back_Model_Roles();
        $select = new Zend_Db_Table_Select($model->_getMapper()->_getTable());
        
        $form = null;
        
        $this->view->tableIdField = 'id_roles';
        $this->view->tableSelect = $select;
        $this->view->tableHeader = array(
                'id_roles' => array(
                        'name' => 'id',
                        'link' => array(
                                'action' => 'editer'
                        )
                ),
                'nom' => array(
                        'name' => __("Nom"),
                        'link' => array(
                                'action' => 'editer'
                        )
                )
        );
        
        /*
         * Thierry BROUARD (BIIS) callback = array( $objet, $method ) link =
         * array( action [,controller[,module]] ) ou array( 'callback' => array(
         * $objet, $method ) )
         */
        $this->view->tableOptions = array(
                'callback' => array(
                        $this,
                        '_transformRowsCallback'
                ), // Transformation callback
                'link' => array(
                        'action' => 'voir'
                ),
                'pageNumber' => (int) $this->_getParam('page', 1),
                'pageItemPerPage' => (int) $this->_getParam('par', 10),
                'form' => $form
        );
        
    }

    public function editerAction ()
    {
        $requete = $this->getRequest();
        $id = $requete->getParam("id", 0);
        
        $form = new Back_Form_Roles('Formulairejndpro', array());
        $model = new Back_Model_Roles($id);
        
        if ($requete->isPost()) {
            if ($form->isValid($requete->getPost())) {
                $date = date(Jnd_Global_Constantes::FORMAT_DATE_BASE);
                if (intval($id) > 0) {
                    $model->charger($id);
                    $model->setOptions($form->getValues());
                } else {
                    $model->setOptions($form->getValues());
                }
                $resultat = $model->sauver();
                $id = $model->getId();
                
                return $this->_helper->redirector('index');
            }
        } elseif ($requete->isGet()) {
            if ($id > 0) {
                $model->charger($id);
                $form->populate($model->toArray());
            }
        }
        $this->view->form = $form;
        $this->view->model = $model;
    }

    /**
     *
     * @see Jnd_Controller_Action::_transformRowsCallback()
     */
    public function _transformRowsCallback ($row, $view)
    {}
}

